##
# Author: Samuel Abels <http://debain.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
##
import gtk, os.path
from util             import find_db_file
from WorkflowDB       import WorkflowDB
from MainWindow       import MainWindow
from WorkflowEditor   import WorkflowEditor
from WorkflowSelector import WorkflowSelector

class MainController(object):
    def __init__(self):
        self.workflow_db = WorkflowDB(find_db_file())
        self.mainwindow  = MainWindow(self)
        self.mainwindow.connect('select-workflow',
                                self._on_mainwindow_select_workflow)
        self.mainwindow.connect('edit-workflow',
                                self._on_mainwindow_edit_workflow)


    def _run_workflow_selector(self):
        selector = WorkflowSelector(self, True)
        selector.connect('cancel', self._on_dialog_cancel)
        selector.connect('edit',   self._on_selector_edit)
        selector.connect('select', self._on_selector_select)


    def _run_workflow_editor(self, workflow):
        editor = WorkflowEditor(self, workflow)
        editor.connect('cancel', self._on_dialog_cancel)
        editor.connect('save',   self._on_editor_save)


    def workflow_opened(self, workflow):
        uri     = 'workflow:' + workflow.get_name()
        manager = gtk.recent_manager_get_default()
        binary  = os.path.join(os.path.dirname(__file__), 'nanny.py')
        data    = dict(display_name = str(workflow.get_name()),
                       description  = str(workflow.get_summary()),
                       mime_type    = 'application/nanny',
                       app_name     = 'nanny',
                       app_exec     = binary,
                       is_private   = False)
        manager.add_full(uri, data)


    ################################################################
    # Callbacks.
    ################################################################
    def _on_mainwindow_select_workflow(self, window):
        self._run_workflow_selector()


    def _on_mainwindow_edit_workflow(self, window, workflow):
        self._run_workflow_editor(workflow)


    def _on_dialog_cancel(self, dialog):
        dialog.hide()


    def _on_selector_edit(self, selector, workflow):
        selector.hide()
        self._run_workflow_editor(workflow)


    def _on_selector_select(self, selector, workflow):
        selector.hide()
        self.mainwindow.open_workflow(workflow)


    def _on_editor_save(self, editor, workflow):
        editor.hide()
        self.mainwindow.workflow_changed(workflow)
